"use strict";

let Diamond = function(cxt1,type,x,y){
	let that = {
		cxt : cxt1,
		x : x,
		y : y,
		row : 0,
		col : 0,
		diamondTime : 500,
		diamondLazy : 0,
		type : type,
		diamondIndex : 0,
		diamondObj : null,
		draw : function(){
			this.diamondObj = DiamondData[type][that.diamondIndex];
			for(let i = 0; i < 4;i++){
				for(let j = 0;j<this.diamondObj.diamonds.length;j++){
					if(this.diamondObj.diamonds[j][i] == 1){
						Canvas.fillRect(GameManager.canvasList.diamond,i*25+5+this.x,j*25+5+this.y,15,15,'black');
					}
				}
			}
		},
		redenrDiamond : function(){
			if(that.diamondIndex == DiamondData[type].length-1){
				that.diamondIndex = 0;
			}else{
				that.diamondIndex += 1;
			}
		},
		update : function(){
			let position = this.calculateRowCol();

			if(!that.checkDown(position)){
				return;
			}
			
			if(that.diamondLazy > 0){
				that.diamondLazy -= GameManager.timeInterval;
				return false;
			}else{
				that.diamondLazy = that.diamondTime;
			}

			this.y += 25;
		},
		checkLeft : function(position){
			position = position || this.calculateRowCol()
			if (!this.diamondObj)return false;
			if (position.col <= 0){
				return;
			}

			position.col -= 1;

			let canExec = true;

			for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
				for (let j = 0; j < 4; j++) {
					let extRow = position.row + i;
					let extCol = position.col + j;
					if (this.diamondObj.diamonds[i][j] != 0 && BackgroundHelper.mapCode[extRow][extCol] !== 0) {
						canExec = false;
						break;
					}
				}
			}

			if (!canExec){
				return;
			}

			this.x -= 25;
		},
		checkRight : function(position){
			position = position || this.calculateRowCol()
			if (!this.diamondObj)return false;
			if (position.col >= GameManager.col -1){
				return;
			}

			if (position.col + this.diamondObj.width > GameManager.col - 1){
				return;
			}
			position.col += 1;
			let canExec = true;
			for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
				for (let j = 0; j < 4; j++) {
					let extRow = position.row + i;
					let extCol = position.col + j;
					if (this.diamondObj.diamonds[i][j] != 0 && BackgroundHelper.mapCode[extRow][extCol] !== 0) {
						canExec = false;
						break;
					}
				}
			}

			if (!canExec){
				return;
			}

			this.x += 25;
		},
		checkDown : function(position){
			position = position || this.calculateRowCol()

			if (this.check(position.row, position.col)) {
				return true;
			} else {
				if(position.row === 1){
					GameManager.over();
					return false;
				}
				GameManager.diamondList.remove(this);
				this.renderMap();
				GameManager.initDiamond();
				return false;
			}
		},
		check : function(row,col){
			if (!this.diamondObj)return false;
			if(row >= GameManager.row-1){
				return false;
			}
			if(row + this.diamondObj.height > GameManager.row - 1){
				return false;
			}
			//下探一行
			row += 1;
			for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
			  for (let j = 0; j < 4; j++) {
				  let extRow = row + i;
				if (this.diamondObj.diamonds[i][j] != 0 && BackgroundHelper.mapCode[extRow][col + j] !== 0) {
				  return false;
				}
			  }
			}
			return true;
		},
		calculateRowCol : function(){
			this.row = this.y / 25;
			this.col = this.x / 25;
			return {"row":this.row,"col":this.col};
		},
		renderMap : function(){
			if (!this.diamondObj)return false;
			for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
				for (let j = 0; j < 4; j++) {
					//   将现在已有的方块渲染到Map类的mapCode上
					if (this.diamondObj.diamonds[i][j] !== 0) {
						let extRow = this.row + i;
						if(extRow>GameManager.row-1){
							extRow = GameManager.row-1;
						}
						let extCol = this.col + j;
						if(extCol>GameManager.col-1){
							extCol = GameManager.col-1;
						}
					  // 改变地图的mapCode数据
					  BackgroundHelper.mapCode[extRow][extCol] = this.diamondObj.diamonds[i][j];
					}
				}
			}
		}
	};
	return that;
};